<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 技术教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="以前看过的关于 RabbitMQ 核心消息模式的文章都是基于 Java API 的，最近看了下官方文档，发现这些核心消息模式都可以通过 Spring AMQP 来实现。于是总结了下 RabbitMQ 的核心知识点，包括 RabbitMQ 在 Windows 和 Linux 下的安装、5种核心消息模式的 Spring AMQP 实现，相信对于想要学习和回顾 RabbitMQ 的朋友都会有所帮助。">
<meta property="og:type" content="article">
<meta property="og:title" content="连RabbitMQ的5种核心消息模式都不懂，也敢说自己会用消息队列！">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2024/01/ebe153280823.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="以前看过的关于 RabbitMQ 核心消息模式的文章都是基于 Java API 的，最近看了下官方文档，发现这些核心消息模式都可以通过 Spring AMQP 来实现。于是总结了下 RabbitMQ 的核心知识点，包括 RabbitMQ 在 Windows 和 Linux 下的安装、5种核心消息模式的 Spring AMQP 实现，相信对于想要学习和回顾 RabbitMQ 的朋友都会有所帮助。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2024-01-21T09:28:49.000Z">
<meta property="article:modified_time" content="2024-02-02T13:40:55.045Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="宅男,热血">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2024/01/ebe153280823.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 技术教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-02 21:40:55'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">连RabbitMQ的5种核心消息模式都不懂，也敢说自己会用消息队列！</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-21T09:28:49.000Z" title="发表于 2024-01-21 17:28:49">2024-01-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-02T13:40:55.045Z" title="更新于 2024-02-02 21:40:55">2024-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/">技术教程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/">mall</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/%E5%8F%82%E8%80%83%E7%AF%87/">参考篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>19分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="连RabbitMQ的5种核心消息模式都不懂，也敢说自己会用消息队列！"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><blockquote>
<p>以前看过的关于 RabbitMQ 核心消息模式的文章都是基于 Java API 的，最近看了下官方文档，发现这些核心消息模式都可以通过 Spring AMQP 来实现。于是总结了下 RabbitMQ 的核心知识点，包括 RabbitMQ 在 Windows 和 Linux 下的安装、5种核心消息模式的 Spring AMQP 实现，相信对于想要学习和回顾 RabbitMQ 的朋友都会有所帮助。</p>
</blockquote>
<h2 id="简介"><a href="#简介" class="headerlink" title="#简介"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E7%AE%80%E4%BB%8B">#</a>简介</h2><p>RabbitMQ是最受欢迎的开源消息中间件之一，在全球范围内被广泛应用。RabbitMQ是轻量级且易于部署的，能支持多种消息协议。RabbitMQ可以部署在分布式系统中，以满足大规模、高可用的要求。</p>
<h2 id="相关概念"><a href="#相关概念" class="headerlink" title="#相关概念"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5">#</a>相关概念</h2><p>我们先来了解下RabbitMQ中的相关概念，这里以5种消息模式中的<code>路由模式</code>为例。</p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_01-a2c603c3.png"></p>
<table>
<thead>
<tr>
<th>标志</th>
<th>中文名</th>
<th>英文名</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>P</td>
<td>生产者</td>
<td>Producer</td>
<td>消息的发送者，可以将消息发送到交换机</td>
</tr>
<tr>
<td>C</td>
<td>消费者</td>
<td>Consumer</td>
<td>消息的接收者，从队列中获取消息并进行消费</td>
</tr>
<tr>
<td>X</td>
<td>交换机</td>
<td>Exchange</td>
<td>接收生产者发送的消息，并根据路由键发送给指定队列</td>
</tr>
<tr>
<td>Q</td>
<td>队列</td>
<td>Queue</td>
<td>存储从交换机发来的消息</td>
</tr>
<tr>
<td>type</td>
<td>交换机类型</td>
<td>type</td>
<td>不同类型的交换机转发消息方式不同</td>
</tr>
<tr>
<td>fanout</td>
<td>发布&#x2F;订阅模式</td>
<td>fanout</td>
<td>广播消息给所有绑定交换机的队列</td>
</tr>
<tr>
<td>direct</td>
<td>路由模式</td>
<td>direct</td>
<td>根据路由键发送消息</td>
</tr>
<tr>
<td>topic</td>
<td>通配符模式</td>
<td>topic</td>
<td>根据路由键的匹配规则发送消息</td>
</tr>
</tbody></table>
<h2 id="安装及配置"><a href="#安装及配置" class="headerlink" title="#安装及配置"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E5%AE%89%E8%A3%85%E5%8F%8A%E9%85%8D%E7%BD%AE">#</a>安装及配置</h2><blockquote>
<p>接下来我们介绍下RabbitMQ的安装和配置，提供Windows和Linux两种安装方式。</p>
</blockquote>
<h3 id="Windows下的安装"><a href="#Windows下的安装" class="headerlink" title="#Windows下的安装"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#windows%E4%B8%8B%E7%9A%84%E5%AE%89%E8%A3%85">#</a>Windows下的安装</h3><ul>
<li>安装Erlang，下载地址：<a target="_blank" rel="noopener" href="http://erlang.org/download/otp_win64_21.3.exe">http://erlang.org/download/otp_win64_21.3.exe</a></li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_02-ca8546a1.png"></p>
<ul>
<li>安装RabbitMQ，下载地址：<a target="_blank" rel="noopener" href="https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe">https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe</a></li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_03-85303a24.png"></p>
<ul>
<li>安装完成后，进入RabbitMQ安装目录下的sbin目录；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_04-032eb75c.png"></p>
<ul>
<li>在地址栏输入cmd并回车启动命令行，然后输入以下命令启动管理功能。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rabbitmq-plugins enable rabbitmq_management</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_05-5f85cade.png"></p>
<h3 id="Linux下的安装"><a href="#Linux下的安装" class="headerlink" title="#Linux下的安装"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#linux%E4%B8%8B%E7%9A%84%E5%AE%89%E8%A3%85">#</a>Linux下的安装</h3><ul>
<li>下载<code>rabbitmq 3.7.15</code>的Docker镜像；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull rabbitmq:3.7.15</span><br></pre></td></tr></table></figure>

<ul>
<li>使用Docker命令启动服务；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker run -p 5672:5672 -p 15672:15672 --name rabbitmq \</span><br><span class="line">-d rabbitmq:3.7.15</span><br></pre></td></tr></table></figure>

<ul>
<li>进入容器并开启管理功能；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker exec -it rabbitmq /bin/bash</span><br><span class="line">rabbitmq-plugins enable rabbitmq_management</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_11-f3f2be24.png"></p>
<ul>
<li>开启防火墙便于外网访问。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --zone=public --add-port=15672/tcp --permanent</span><br><span class="line">firewall-cmd --zone=public --add-port=5672/tcp --permanent</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>

<h3 id="访问及配置"><a href="#访问及配置" class="headerlink" title="#访问及配置"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E8%AE%BF%E9%97%AE%E5%8F%8A%E9%85%8D%E7%BD%AE">#</a>访问及配置</h3><ul>
<li>访问RabbitMQ管理页面地址，查看是否安装成功（Linux下使用服务器IP访问即可）：<a target="_blank" rel="noopener" href="http://localhost:15672/">http://localhost:15672/</a></li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_06-56a54ec6.png"></p>
<ul>
<li><p>输入账号密码并登录，这里使用默认账号密码登录：guest guest</p>
</li>
<li><p>创建帐号并设置其角色为管理员：mall mall</p>
</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_07-88ce83da.png"></p>
<ul>
<li>创建一个新的虚拟host为：&#x2F;mall</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_08-8220cac4.png"></p>
<ul>
<li>点击mall用户进入用户配置页面；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_09-5248b5a7.png"></p>
<ul>
<li>给mall用户配置该虚拟host的权限；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_10-df67b8d9.png"></p>
<ul>
<li>至此，RabbitMQ的配置完成。</li>
</ul>
<h2 id="5种消息模式"><a href="#5种消息模式" class="headerlink" title="#5种消息模式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#_5%E7%A7%8D%E6%B6%88%E6%81%AF%E6%A8%A1%E5%BC%8F">#</a>5种消息模式</h2><blockquote>
<p>这5种消息模式是构建基于RabbitMQ的消息应用的基础，一定要牢牢掌握它们。学过RabbitMQ的朋友应该了解过这些消息模式的Java实现，这里我们使用Spring AMQP的形式来实现它们。</p>
</blockquote>
<h3 id="简单模式"><a href="#简单模式" class="headerlink" title="#简单模式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E7%AE%80%E5%8D%95%E6%A8%A1%E5%BC%8F">#</a>简单模式</h3><blockquote>
<p>简单模式是最简单的消息模式，它包含一个生产者、一个消费者和一个队列。生产者向队列里发送消息，消费者从队列中获取消息并消费。</p>
</blockquote>
<h4 id="模式示意图"><a href="#模式示意图" class="headerlink" title="#模式示意图"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE">#</a>模式示意图</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_12-7838ccd5.png"></p>
<h4 id="Spring-AMQP实现"><a href="#Spring-AMQP实现" class="headerlink" title="#Spring AMQP实现"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#spring-amqp%E5%AE%9E%E7%8E%B0">#</a>Spring AMQP实现</h4><ul>
<li>首先需要在<code>pom.xml</code>中添加Spring AMQP的相关依赖；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--Spring AMQP依赖--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-amqp&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>然后修改<code>application.yml</code>，添加RabbitMQ的相关配置；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  rabbitmq:</span><br><span class="line">    host: localhost</span><br><span class="line">    port: 5672</span><br><span class="line">    virtual-host: /mall</span><br><span class="line">    username: mall</span><br><span class="line">    password: mall</span><br><span class="line">    publisher-confirms: true #消息发送到交换器确认</span><br><span class="line">    publisher-returns: true #消息发送到队列确认</span><br></pre></td></tr></table></figure>

<ul>
<li>添加<code>简单模式</code>相关Java配置，创建一个名为<code>simple.hello</code>的队列、一个生产者和一个消费者；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Configuration</span><br><span class="line">public class SimpleRabbitConfig &#123;</span><br><span class="line"></span><br><span class="line">	@Bean</span><br><span class="line">	public Queue hello() &#123;</span><br><span class="line">		return new Queue(&quot;simple.hello&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Bean</span><br><span class="line">	public SimpleSender simpleSender()&#123;</span><br><span class="line">		return new SimpleSender();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Bean</span><br><span class="line">	public SimpleReceiver simpleReceiver()&#123;</span><br><span class="line">		return new SimpleReceiver();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>生产者通过<code>send方法</code>向队列<code>simple.hello</code>中发送消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class SimpleSender &#123;</span><br><span class="line"></span><br><span class="line">	private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSender.class);</span><br><span class="line"></span><br><span class="line">	@Autowired</span><br><span class="line">	private RabbitTemplate template;</span><br><span class="line"></span><br><span class="line">	private static final String queueName=&quot;simple.hello&quot;;</span><br><span class="line"></span><br><span class="line">	public void send() &#123;</span><br><span class="line">		String message = &quot;Hello World!&quot;;</span><br><span class="line">		this.template.convertAndSend(queueName, message);</span><br><span class="line">		LOGGER.info(&quot; [x] Sent &#x27;&#123;&#125;&#x27;&quot;, message);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>消费者从队列<code>simple.hello</code>中获取消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@RabbitListener(queues = &quot;simple.hello&quot;)</span><br><span class="line">public class SimpleReceiver &#123;</span><br><span class="line"></span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReceiver.class);</span><br><span class="line"></span><br><span class="line">    @RabbitHandler</span><br><span class="line">    public void receive(String in) &#123;</span><br><span class="line">        LOGGER.info(&quot; [x] Received &#x27;&#123;&#125;&#x27;&quot;, in);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>在controller中添加测试接口，调用该接口开始发送消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Api(tags = &quot;RabbitController&quot;, description = &quot;RabbitMQ功能测试&quot;)</span><br><span class="line">@Controller</span><br><span class="line">@RequestMapping(&quot;/rabbit&quot;)</span><br><span class="line">public class RabbitController &#123;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private SimpleSender simpleSender;</span><br><span class="line"></span><br><span class="line">    @ApiOperation(&quot;简单模式&quot;)</span><br><span class="line">    @RequestMapping(value = &quot;/simple&quot;, method = RequestMethod.GET)</span><br><span class="line">    @ResponseBody</span><br><span class="line">    public CommonResult simpleTest() &#123;</span><br><span class="line">        for(int i=0;i&lt;10;i++)&#123;</span><br><span class="line">            simpleSender.send();</span><br><span class="line">            ThreadUtil.sleep(1000);</span><br><span class="line">        &#125;</span><br><span class="line">        return CommonResult.success(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>运行后结果如下，可以发现生产者往队列中发送消息，消费者从队列中获取消息并消费。</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_13-2f4f992a.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_14-c1ecbbd1.png"></p>
<h3 id="工作模式"><a href="#工作模式" class="headerlink" title="#工作模式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F">#</a>工作模式</h3><blockquote>
<p>工作模式是指向多个互相竞争的消费者发送消息的模式，它包含一个生产者、两个消费者和一个队列。两个消费者同时绑定到一个队列上去，当消费者获取消息处理耗时任务时，空闲的消费者从队列中获取并消费消息。</p>
</blockquote>
<h4 id="模式示意图-1"><a href="#模式示意图-1" class="headerlink" title="#模式示意图"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-1">#</a>模式示意图</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_15-0c07a406.png"></p>
<h4 id="Spring-AMQP实现-1"><a href="#Spring-AMQP实现-1" class="headerlink" title="#Spring AMQP实现"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#spring-amqp%E5%AE%9E%E7%8E%B0-1">#</a>Spring AMQP实现</h4><ul>
<li>添加<code>工作模式</code>相关Java配置，创建一个名为<code>work.hello</code>的队列、一个生产者和两个消费者；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Configuration</span><br><span class="line">public class WorkRabbitConfig &#123;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue workQueue() &#123;</span><br><span class="line">        return new Queue(&quot;work.hello&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public WorkReceiver workReceiver1() &#123;</span><br><span class="line">        return new WorkReceiver(1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public WorkReceiver workReceiver2() &#123;</span><br><span class="line">        return new WorkReceiver(2);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public WorkSender workSender() &#123;</span><br><span class="line">        return new WorkSender();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>生产者通过<code>send方法</code>向队列<code>work.hello</code>中发送消息，消息中包含一定数量的<code>.</code>号；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class WorkSender &#123;</span><br><span class="line"></span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(WorkSender.class);</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private RabbitTemplate template;</span><br><span class="line"></span><br><span class="line">    private static final String queueName = &quot;work.hello&quot;;</span><br><span class="line"></span><br><span class="line">    public void send(int index) &#123;</span><br><span class="line">        StringBuilder builder = new StringBuilder(&quot;Hello&quot;);</span><br><span class="line">        int limitIndex = index % 3+1;</span><br><span class="line">        for (int i = 0; i &lt; limitIndex; i++) &#123;</span><br><span class="line">            builder.append(&#x27;.&#x27;);</span><br><span class="line">        &#125;</span><br><span class="line">        builder.append(index+1);</span><br><span class="line">        String message = builder.toString();</span><br><span class="line">        template.convertAndSend(queueName, message);</span><br><span class="line">        LOGGER.info(&quot; [x] Sent &#x27;&#123;&#125;&#x27;&quot;, message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>两个消费者从队列<code>work.hello</code>中获取消息，名称分别为<code>instance 1</code>和<code>instance 2</code>，消息中包含<code>.</code>号越多，耗时越长；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@RabbitListener(queues = &quot;work.hello&quot;)</span><br><span class="line">public class WorkReceiver &#123;</span><br><span class="line"></span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(WorkReceiver.class);</span><br><span class="line"></span><br><span class="line">    private final int instance;</span><br><span class="line"></span><br><span class="line">    public WorkReceiver(int i) &#123;</span><br><span class="line">        this.instance = i;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RabbitHandler</span><br><span class="line">    public void receive(String in) &#123;</span><br><span class="line">        StopWatch watch = new StopWatch();</span><br><span class="line">        watch.start();</span><br><span class="line">        LOGGER.info(&quot;instance &#123;&#125; [x] Received &#x27;&#123;&#125;&#x27;&quot;, this.instance, in);</span><br><span class="line">        doWork(in);</span><br><span class="line">        watch.stop();</span><br><span class="line">        LOGGER.info(&quot;instance &#123;&#125; [x] Done in &#123;&#125;s&quot;, this.instance, watch.getTotalTimeSeconds());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void doWork(String in) &#123;</span><br><span class="line">        for (char ch : in.toCharArray()) &#123;</span><br><span class="line">            if (ch == &#x27;.&#x27;) &#123;</span><br><span class="line">                ThreadUtil.sleep(1000);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>在controller中添加测试接口，调用该接口开始发送消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Api(tags = &quot;RabbitController&quot;, description = &quot;RabbitMQ功能测试&quot;)</span><br><span class="line">@Controller</span><br><span class="line">@RequestMapping(&quot;/rabbit&quot;)</span><br><span class="line">public class RabbitController &#123;</span><br><span class="line">    </span><br><span class="line">    @Autowired</span><br><span class="line">    private WorkSender workSender;</span><br><span class="line"></span><br><span class="line">    @ApiOperation(&quot;工作模式&quot;)</span><br><span class="line">    @RequestMapping(value = &quot;/work&quot;, method = RequestMethod.GET)</span><br><span class="line">    @ResponseBody</span><br><span class="line">    public CommonResult workTest() &#123;</span><br><span class="line">        for(int i=0;i&lt;10;i++)&#123;</span><br><span class="line">            workSender.send(i);</span><br><span class="line">            ThreadUtil.sleep(1000);</span><br><span class="line">        &#125;</span><br><span class="line">        return CommonResult.success(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>运行后结果如下，可以发现生产者往队列中发送包含不同数量<code>.</code>号的消息，<code>instance 1</code>和<code>instance 2</code>消费者互相竞争，分别消费了一部分消息。</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_16-622a4da6.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_17-6aafc84b.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_18-801e791c.png"></p>
<h3 id="发布-订阅模式"><a href="#发布-订阅模式" class="headerlink" title="#发布&#x2F;订阅模式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E5%8F%91%E5%B8%83-%E8%AE%A2%E9%98%85%E6%A8%A1%E5%BC%8F">#</a>发布&#x2F;订阅模式</h3><blockquote>
<p>发布&#x2F;订阅模式是指同时向多个消费者发送消息的模式（类似广播的形式），它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去，两个队列绑定到交换机上去，生产者通过发送消息到交换机，所有消费者接收并消费消息。</p>
</blockquote>
<h4 id="模式示意图-2"><a href="#模式示意图-2" class="headerlink" title="#模式示意图"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-2">#</a>模式示意图</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_19-0430b52e.png"></p>
<h4 id="Spring-AMQP实现-2"><a href="#Spring-AMQP实现-2" class="headerlink" title="#Spring AMQP实现"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#spring-amqp%E5%AE%9E%E7%8E%B0-2">#</a>Spring AMQP实现</h4><ul>
<li>添加<code>发布/订阅模式</code>相关Java配置，创建一个名为<code>exchange.fanout</code>的交换机、一个生产者、两个消费者和两个匿名队列，将两个匿名队列都绑定到交换机；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Configuration</span><br><span class="line">public class FanoutRabbitConfig &#123;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public FanoutExchange fanout() &#123;</span><br><span class="line">        return new FanoutExchange(&quot;exchange.fanout&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue fanoutQueue1() &#123;</span><br><span class="line">        return new AnonymousQueue();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue fanoutQueue2() &#123;</span><br><span class="line">        return new AnonymousQueue();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding fanoutBinding1(FanoutExchange fanout, Queue fanoutQueue1) &#123;</span><br><span class="line">        return BindingBuilder.bind(fanoutQueue1).to(fanout);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding fanoutBinding2(FanoutExchange fanout, Queue fanoutQueue2) &#123;</span><br><span class="line">        return BindingBuilder.bind(fanoutQueue2).to(fanout);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public FanoutReceiver fanoutReceiver() &#123;</span><br><span class="line">        return new FanoutReceiver();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public FanoutSender fanoutSender() &#123;</span><br><span class="line">        return new FanoutSender();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>生产者通过<code>send方法</code>向交换机<code>exchange.fanout</code>中发送消息，消息中包含一定数量的<code>.</code>号；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class FanoutSender &#123;</span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(FanoutSender.class);</span><br><span class="line">    @Autowired</span><br><span class="line">    private RabbitTemplate template;</span><br><span class="line"></span><br><span class="line">    private static final String exchangeName = &quot;exchange.fanout&quot;;</span><br><span class="line"></span><br><span class="line">    public void send(int index) &#123;</span><br><span class="line">        StringBuilder builder = new StringBuilder(&quot;Hello&quot;);</span><br><span class="line">        int limitIndex = index % 3 + 1;</span><br><span class="line">        for (int i = 0; i &lt; limitIndex; i++) &#123;</span><br><span class="line">            builder.append(&#x27;.&#x27;);</span><br><span class="line">        &#125;</span><br><span class="line">        builder.append(index + 1);</span><br><span class="line">        String message = builder.toString();</span><br><span class="line">        template.convertAndSend(exchangeName, &quot;&quot;, message);</span><br><span class="line">        LOGGER.info(&quot; [x] Sent &#x27;&#123;&#125;&#x27;&quot;, message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>消费者从绑定的匿名队列中获取消息，消息中包含<code>.</code>号越多，耗时越长，由于该消费者可以从两个队列中获取并消费消息，可以看做两个消费者，名称分别为<code>instance 1</code>和<code>instance 2</code>；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class FanoutReceiver &#123;</span><br><span class="line"></span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(FanoutReceiver.class);</span><br><span class="line"></span><br><span class="line">    @RabbitListener(queues = &quot;#&#123;fanoutQueue1.name&#125;&quot;)</span><br><span class="line">    public void receive1(String in) &#123;</span><br><span class="line">        receive(in, 1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RabbitListener(queues = &quot;#&#123;fanoutQueue2.name&#125;&quot;)</span><br><span class="line">    public void receive2(String in) &#123;</span><br><span class="line">        receive(in, 2);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void receive(String in, int receiver) &#123;</span><br><span class="line">        StopWatch watch = new StopWatch();</span><br><span class="line">        watch.start();</span><br><span class="line">        LOGGER.info(&quot;instance &#123;&#125; [x] Received &#x27;&#123;&#125;&#x27;&quot;, receiver, in);</span><br><span class="line">        doWork(in);</span><br><span class="line">        watch.stop();</span><br><span class="line">        LOGGER.info(&quot;instance &#123;&#125; [x] Done in &#123;&#125;s&quot;, receiver, watch.getTotalTimeSeconds());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void doWork(String in) &#123;</span><br><span class="line">        for (char ch : in.toCharArray()) &#123;</span><br><span class="line">            if (ch == &#x27;.&#x27;) &#123;</span><br><span class="line">                ThreadUtil.sleep(1000);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>在controller中添加测试接口，调用该接口开始发送消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Api(tags = &quot;RabbitController&quot;, description = &quot;RabbitMQ功能测试&quot;)</span><br><span class="line">@Controller</span><br><span class="line">@RequestMapping(&quot;/rabbit&quot;)</span><br><span class="line">public class RabbitController &#123;</span><br><span class="line">    </span><br><span class="line">    @Autowired</span><br><span class="line">    private FanoutSender fanoutSender;</span><br><span class="line"></span><br><span class="line">    @ApiOperation(&quot;发布/订阅模式&quot;)</span><br><span class="line">    @RequestMapping(value = &quot;/fanout&quot;, method = RequestMethod.GET)</span><br><span class="line">    @ResponseBody</span><br><span class="line">    public CommonResult fanoutTest() &#123;</span><br><span class="line">        for(int i=0;i&lt;10;i++)&#123;</span><br><span class="line">            fanoutSender.send(i);</span><br><span class="line">            ThreadUtil.sleep(1000);</span><br><span class="line">        &#125;</span><br><span class="line">        return CommonResult.success(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>运行后结果如下，可以发现生产者往队列中发送包含不同数量<code>.</code>号的消息，<code>instance 1</code>和<code>instance 2</code>同时获取并消费了消息。</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_20-1f361f7e.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_21-eb9e78f0.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_22-06ab58ce.png"></p>
<h3 id="路由模式"><a href="#路由模式" class="headerlink" title="#路由模式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%BC%8F">#</a>路由模式</h3><blockquote>
<p>路由模式是可以根据<code>路由键</code>选择性给多个消费者发送消息的模式，它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去，两个队列通过<code>路由键</code>绑定到交换机上去，生产者发送消息到交换机，交换机通过<code>路由键</code>转发到不同队列，队列绑定的消费者接收并消费消息。</p>
</blockquote>
<h4 id="模式示意图-3"><a href="#模式示意图-3" class="headerlink" title="#模式示意图"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-3">#</a>模式示意图</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_23-77811fd1.png"></p>
<h4 id="Spring-AMQP实现-3"><a href="#Spring-AMQP实现-3" class="headerlink" title="#Spring AMQP实现"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#spring-amqp%E5%AE%9E%E7%8E%B0-3">#</a>Spring AMQP实现</h4><ul>
<li>添加<code>路由模式</code>相关Java配置，创建一个名为<code>exchange.direct</code>的交换机、一个生产者、两个消费者和两个匿名队列，队列通过<code>路由键</code>都绑定到交换机，<code>队列1</code>的路由键为<code>orange</code>和<code>black</code>，<code>队列2</code>的路由键为<code>green</code>和<code>black</code>；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Configuration</span><br><span class="line">public class DirectRabbitConfig &#123;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public DirectExchange direct() &#123;</span><br><span class="line">        return new DirectExchange(&quot;exchange.direct&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue directQueue1() &#123;</span><br><span class="line">        return new AnonymousQueue();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue directQueue2() &#123;</span><br><span class="line">        return new AnonymousQueue();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding directBinding1a(DirectExchange direct, Queue directQueue1) &#123;</span><br><span class="line">        return BindingBuilder.bind(directQueue1).to(direct).with(&quot;orange&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding directBinding1b(DirectExchange direct, Queue directQueue1) &#123;</span><br><span class="line">        return BindingBuilder.bind(directQueue1).to(direct).with(&quot;black&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding directBinding2a(DirectExchange direct, Queue directQueue2) &#123;</span><br><span class="line">        return BindingBuilder.bind(directQueue2).to(direct).with(&quot;green&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding directBinding2b(DirectExchange direct, Queue directQueue2) &#123;</span><br><span class="line">        return BindingBuilder.bind(directQueue2).to(direct).with(&quot;black&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public DirectReceiver receiver() &#123;</span><br><span class="line">        return new DirectReceiver();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public DirectSender directSender() &#123;</span><br><span class="line">        return new DirectSender();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>生产者通过<code>send方法</code>向交换机<code>exchange.direct</code>中发送消息，发送时使用不同的<code>路由键</code>，根据<code>路由键</code>会被转发到不同的队列；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class DirectSender &#123;</span><br><span class="line"></span><br><span class="line">	@Autowired</span><br><span class="line">	private RabbitTemplate template;</span><br><span class="line"></span><br><span class="line">	private static final String exchangeName = &quot;exchange.direct&quot;;</span><br><span class="line"></span><br><span class="line">	private final String[] keys = &#123;&quot;orange&quot;, &quot;black&quot;, &quot;green&quot;&#125;;</span><br><span class="line"></span><br><span class="line">	private static final Logger LOGGER = LoggerFactory.getLogger(DirectSender.class);</span><br><span class="line"></span><br><span class="line">	public void send(int index) &#123;</span><br><span class="line">		StringBuilder builder = new StringBuilder(&quot;Hello to &quot;);</span><br><span class="line">		int limitIndex = index % 3;</span><br><span class="line">		String key = keys[limitIndex];</span><br><span class="line">		builder.append(key).append(&#x27; &#x27;);</span><br><span class="line">		builder.append(index+1);</span><br><span class="line">		String message = builder.toString();</span><br><span class="line">		template.convertAndSend(exchangeName, key, message);</span><br><span class="line">		LOGGER.info(&quot; [x] Sent &#x27;&#123;&#125;&#x27;&quot;, message);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>消费者从自己绑定的匿名队列中获取消息，由于该消费者可以从两个队列中获取并消费消息，可以看做两个消费者，名称分别为<code>instance 1</code>和<code>instance 2</code>；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class DirectReceiver &#123;</span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(DirectReceiver.class);</span><br><span class="line"></span><br><span class="line">    @RabbitListener(queues = &quot;#&#123;directQueue1.name&#125;&quot;)</span><br><span class="line">    public void receive1(String in)&#123;</span><br><span class="line">        receive(in, 1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RabbitListener(queues = &quot;#&#123;directQueue2.name&#125;&quot;)</span><br><span class="line">    public void receive2(String in)&#123;</span><br><span class="line">        receive(in, 2);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void receive(String in, int receiver)&#123;</span><br><span class="line">        StopWatch watch = new StopWatch();</span><br><span class="line">        watch.start();</span><br><span class="line">        LOGGER.info(&quot;instance &#123;&#125; [x] Received &#x27;&#123;&#125;&#x27;&quot;, receiver, in);</span><br><span class="line">        doWork(in);</span><br><span class="line">        watch.stop();</span><br><span class="line">        LOGGER.info(&quot;instance &#123;&#125; [x] Done in &#123;&#125;s&quot;, receiver, watch.getTotalTimeSeconds());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void doWork(String in)&#123;</span><br><span class="line">        for (char ch : in.toCharArray()) &#123;</span><br><span class="line">            if (ch == &#x27;.&#x27;) &#123;</span><br><span class="line">                ThreadUtil.sleep(1000);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>在controller中添加测试接口，调用该接口开始发送消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Api(tags = &quot;RabbitController&quot;, description = &quot;RabbitMQ功能测试&quot;)</span><br><span class="line">@Controller</span><br><span class="line">@RequestMapping(&quot;/rabbit&quot;)</span><br><span class="line">public class RabbitController &#123;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private DirectSender directSender;</span><br><span class="line"></span><br><span class="line">    @ApiOperation(&quot;路由模式&quot;)</span><br><span class="line">    @RequestMapping(value = &quot;/direct&quot;, method = RequestMethod.GET)</span><br><span class="line">    @ResponseBody</span><br><span class="line">    public CommonResult directTest() &#123;</span><br><span class="line">        for(int i=0;i&lt;10;i++)&#123;</span><br><span class="line">            directSender.send(i);</span><br><span class="line">            ThreadUtil.sleep(1000);</span><br><span class="line">        &#125;</span><br><span class="line">        return CommonResult.success(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>运行后结果如下，可以发现生产者往队列中发送包含不同<code>路由键</code>的消息，<code>instance 1</code>获取到了<code>orange</code>和<code>black</code>消息，<code>instance 2</code>获取到了<code>green</code>和<code>black</code>消息。</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_24-e47d1ea5.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_25-2147e0a1.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_26-d7a806a8.png"></p>
<h3 id="通配符模式"><a href="#通配符模式" class="headerlink" title="#通配符模式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E9%80%9A%E9%85%8D%E7%AC%A6%E6%A8%A1%E5%BC%8F">#</a>通配符模式</h3><blockquote>
<p>通配符模式是可以根据<code>路由键匹配规则</code>选择性给多个消费者发送消息的模式，它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去，两个队列通过<code>路由键匹配规则</code>绑定到交换机上去，生产者发送消息到交换机，交换机通过<code>路由键匹配规则</code>转发到不同队列，队列绑定的消费者接收并消费消息。</p>
</blockquote>
<h4 id="特殊匹配符号"><a href="#特殊匹配符号" class="headerlink" title="#特殊匹配符号"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E7%89%B9%E6%AE%8A%E5%8C%B9%E9%85%8D%E7%AC%A6%E5%8F%B7">#</a>特殊匹配符号</h4><ul>
<li><code>*</code>：只能匹配一个单词；</li>
<li><code>#</code>：可以匹配零个或多个单词。</li>
</ul>
<h4 id="模式示意图-4"><a href="#模式示意图-4" class="headerlink" title="#模式示意图"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-4">#</a>模式示意图</h4><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_27-56ab2094.png"></p>
<h4 id="Spring-AMQP实现-4"><a href="#Spring-AMQP实现-4" class="headerlink" title="#Spring AMQP实现"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#spring-amqp%E5%AE%9E%E7%8E%B0-4">#</a>Spring AMQP实现</h4><ul>
<li>添加<code>通配符模式</code>相关Java配置，创建一个名为<code>exchange.topic</code>的交换机、一个生产者、两个消费者和两个匿名队列，匹配<code>*.orange.*</code>和<code>*.*.rabbit</code>发送到<code>队列1</code>，匹配<code>lazy.#</code>发送到<code>队列2</code>；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Configuration</span><br><span class="line">public class TopicRabbitConfig &#123;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public TopicExchange topic() &#123;</span><br><span class="line">        return new TopicExchange(&quot;exchange.topic&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue topicQueue1() &#123;</span><br><span class="line">        return new AnonymousQueue();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Queue topicQueue2() &#123;</span><br><span class="line">        return new AnonymousQueue();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding topicBinding1a(TopicExchange topic, Queue topicQueue1) &#123;</span><br><span class="line">        return BindingBuilder.bind(topicQueue1).to(topic).with(&quot;*.orange.*&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding topicBinding1b(TopicExchange topic, Queue topicQueue1) &#123;</span><br><span class="line">        return BindingBuilder.bind(topicQueue1).to(topic).with(&quot;*.*.rabbit&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public Binding topicBinding2a(TopicExchange topic, Queue topicQueue2) &#123;</span><br><span class="line">        return BindingBuilder.bind(topicQueue2).to(topic).with(&quot;lazy.#&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public TopicReceiver topicReceiver() &#123;</span><br><span class="line">        return new TopicReceiver();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public TopicSender topicSender() &#123;</span><br><span class="line">        return new TopicSender();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>生产者通过<code>send方法</code>向交换机<code>exchange.topic</code>中发送消息，消息中包含不同的<code>路由键</code>；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class TopicSender &#123;</span><br><span class="line"></span><br><span class="line">	@Autowired</span><br><span class="line">	private RabbitTemplate template;</span><br><span class="line"></span><br><span class="line">	private static final String exchangeName = &quot;exchange.topic&quot;;</span><br><span class="line"></span><br><span class="line">	private static final Logger LOGGER = LoggerFactory.getLogger(TopicSender.class);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	private final String[] keys = &#123;&quot;quick.orange.rabbit&quot;, &quot;lazy.orange.elephant&quot;, &quot;quick.orange.fox&quot;,</span><br><span class="line">			&quot;lazy.brown.fox&quot;, &quot;lazy.pink.rabbit&quot;, &quot;quick.brown.fox&quot;&#125;;</span><br><span class="line"></span><br><span class="line">	public void send(int index) &#123;</span><br><span class="line">		StringBuilder builder = new StringBuilder(&quot;Hello to &quot;);</span><br><span class="line">		int limitIndex = index%keys.length;</span><br><span class="line">		String key = keys[limitIndex];</span><br><span class="line">		builder.append(key).append(&#x27; &#x27;);</span><br><span class="line">		builder.append(index+1);</span><br><span class="line">		String message = builder.toString();</span><br><span class="line">		template.convertAndSend(exchangeName, key, message);</span><br><span class="line">		LOGGER.info(&quot; [x] Sent &#x27;&#123;&#125;&#x27;&quot;,message);</span><br><span class="line">		System.out.println(&quot; [x] Sent &#x27;&quot; + message + &quot;&#x27;&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>消费者从自己绑定的匿名队列中获取消息，由于该消费者可以从两个队列中获取并消费消息，可以看做两个消费者，名称分别为<code>instance 1</code>和<code>instance 2</code>；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">public class TopicReceiver &#123;</span><br><span class="line"></span><br><span class="line">	private static final Logger LOGGER = LoggerFactory.getLogger(TopicReceiver.class);</span><br><span class="line"></span><br><span class="line">	@RabbitListener(queues = &quot;#&#123;topicQueue1.name&#125;&quot;)</span><br><span class="line">	public void receive1(String in)&#123;</span><br><span class="line">		receive(in, 1);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@RabbitListener(queues = &quot;#&#123;topicQueue2.name&#125;&quot;)</span><br><span class="line">	public void receive2(String in)&#123;</span><br><span class="line">		receive(in, 2);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void receive(String in, int receiver)&#123;</span><br><span class="line">		StopWatch watch = new StopWatch();</span><br><span class="line">		watch.start();</span><br><span class="line">		LOGGER.info(&quot;instance &#123;&#125; [x] Received &#x27;&#123;&#125;&#x27;&quot;, receiver, in);</span><br><span class="line">		doWork(in);</span><br><span class="line">		watch.stop();</span><br><span class="line">		LOGGER.info(&quot;instance &#123;&#125; [x] Done in &#123;&#125;s&quot;, receiver, watch.getTotalTimeSeconds());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	private void doWork(String in)&#123;</span><br><span class="line">		for (char ch : in.toCharArray()) &#123;</span><br><span class="line">			if (ch == &#x27;.&#x27;) &#123;</span><br><span class="line">				ThreadUtil.sleep(1000);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>在controller中添加测试接口，调用该接口开始发送消息；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/5/19.</span><br><span class="line"> */</span><br><span class="line">@Api(tags = &quot;RabbitController&quot;, description = &quot;RabbitMQ功能测试&quot;)</span><br><span class="line">@Controller</span><br><span class="line">@RequestMapping(&quot;/rabbit&quot;)</span><br><span class="line">public class RabbitController &#123;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private TopicSender topicSender;</span><br><span class="line"></span><br><span class="line">    @ApiOperation(&quot;通配符模式&quot;)</span><br><span class="line">    @RequestMapping(value = &quot;/topic&quot;, method = RequestMethod.GET)</span><br><span class="line">    @ResponseBody</span><br><span class="line">    public CommonResult topicTest() &#123;</span><br><span class="line">        for(int i=0;i&lt;10;i++)&#123;</span><br><span class="line">            topicSender.send(i);</span><br><span class="line">            ThreadUtil.sleep(1000);</span><br><span class="line">        &#125;</span><br><span class="line">        return CommonResult.success(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>运行后结果如下，可以发现生产者往队列中发送包含不同<code>路由键</code>的消息，<code>instance 1</code>和<code>instance 2</code>分别获取到了匹配的消息。</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_28-6bead143.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_29-1d78e643.png"></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/rabbitmq_start_30-7bcbc1bd.png"></p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="#参考资料"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99">#</a>参考资料</h2><p>RabbitMQ Tutorials:<a target="_blank" rel="noopener" href="https://www.rabbitmq.com/getstarted.html">https://www.rabbitmq.com/getstarted.html</a></p>
<h2 id="项目源码地址"><a href="#项目源码地址" class="headerlink" title="#项目源码地址"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/rabbitmq_start.html#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80">#</a>项目源码地址</h2><p><a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-rabbit">https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-rabbit</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2024/01/ebe153280823.html">https://ko25891wan.gitlab.io/2024/01/ebe153280823.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/01/05b706ca527f.html" title="你居然还去服务器上捞日志，搭个日志收集系统难道不香么！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">你居然还去服务器上捞日志，搭个日志收集系统难道不香么！</div></div></a></div><div class="next-post pull-right"><a href="/2024/01/9859ffc20589.html" title="开发者必备MySQL命令"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">开发者必备MySQL命令</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B"><span class="toc-number">1.</span> <span class="toc-text">简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5"><span class="toc-number">2.</span> <span class="toc-text">相关概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E5%8F%8A%E9%85%8D%E7%BD%AE"><span class="toc-number">3.</span> <span class="toc-text">安装及配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Windows%E4%B8%8B%E7%9A%84%E5%AE%89%E8%A3%85"><span class="toc-number">3.1.</span> <span class="toc-text">Windows下的安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Linux%E4%B8%8B%E7%9A%84%E5%AE%89%E8%A3%85"><span class="toc-number">3.2.</span> <span class="toc-text">Linux下的安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E5%8F%8A%E9%85%8D%E7%BD%AE"><span class="toc-number">3.3.</span> <span class="toc-text">访问及配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E7%A7%8D%E6%B6%88%E6%81%AF%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.</span> <span class="toc-text">5种消息模式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.1.</span> <span class="toc-text">简单模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE"><span class="toc-number">4.1.1.</span> <span class="toc-text">模式示意图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Spring-AMQP%E5%AE%9E%E7%8E%B0"><span class="toc-number">4.1.2.</span> <span class="toc-text">Spring AMQP实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.2.</span> <span class="toc-text">工作模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-1"><span class="toc-number">4.2.1.</span> <span class="toc-text">模式示意图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Spring-AMQP%E5%AE%9E%E7%8E%B0-1"><span class="toc-number">4.2.2.</span> <span class="toc-text">Spring AMQP实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%91%E5%B8%83-%E8%AE%A2%E9%98%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.3.</span> <span class="toc-text">发布&#x2F;订阅模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-2"><span class="toc-number">4.3.1.</span> <span class="toc-text">模式示意图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Spring-AMQP%E5%AE%9E%E7%8E%B0-2"><span class="toc-number">4.3.2.</span> <span class="toc-text">Spring AMQP实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.4.</span> <span class="toc-text">路由模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-3"><span class="toc-number">4.4.1.</span> <span class="toc-text">模式示意图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Spring-AMQP%E5%AE%9E%E7%8E%B0-3"><span class="toc-number">4.4.2.</span> <span class="toc-text">Spring AMQP实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%9A%E9%85%8D%E7%AC%A6%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.5.</span> <span class="toc-text">通配符模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%89%B9%E6%AE%8A%E5%8C%B9%E9%85%8D%E7%AC%A6%E5%8F%B7"><span class="toc-number">4.5.1.</span> <span class="toc-text">特殊匹配符号</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E5%BC%8F%E7%A4%BA%E6%84%8F%E5%9B%BE-4"><span class="toc-number">4.5.2.</span> <span class="toc-text">模式示意图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Spring-AMQP%E5%AE%9E%E7%8E%B0-4"><span class="toc-number">4.5.3.</span> <span class="toc-text">Spring AMQP实现</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">5.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80"><span class="toc-number">6.</span> <span class="toc-text">项目源码地址</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>